<?php
//--------------------------------------------------
//	Filename: groups_new.php
//--------------------------------------------------
if( !defined('IN_INDEX') )
{
	printError("ACCESS DENIED", "This page is hidden.");
	exit;
}
if(!checkSession())
{
			function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}

$reg['group_name'] = "";
$reg['group_description']="";
$reg['group_rank']="";
$reg['group_date']="";

$PRIVACY_LIST = array('open', 'closed', 'secret');
$missing = array();
$output ="";

if(isset($_POST['groups_add_submit']))
{
	
	$reg_checked = 0;
	
	$reg['group_name']=$_POST['group_name'];
	$reg['group_description']=$_POST['group_description'];
	$reg['group_rank']=$_POST['group_rank'];
	$reg['group_date']=time();
	
	foreach($reg as $k => $v)
	{
		//echo $k . " | " . $v . "<br />\n";
		
		if(isset($v) && $v != "")
		{
			if($k == "group_name")
			{
					if(checkGroupNameExistance($v))
					{
						$missing[$k] = "is_regged";
					}
					else
					{
						$reg_checked++;
					}
			}
			else
			{
				$reg_checked++;
			}
		}
		else
		{
			$missing[$k] = "missing";
		}	
	}
	
	if($reg_checked >= 4)
	{
	//Create the group
	$sql = "INSERT INTO " . GROUPS_TABLE . " SET ".
		"groups_name='" . $reg['group_name'] . "', ".
		"groups_description='" . $reg['group_description'] . "', ".
		"groups_created_date='" . $reg['group_date'] . "', ".
		"groups_rank='" . $reg['group_rank'] . "'";
	if(!$result = $db->sql_query($sql))
		{
			printError("SQL ERROR", mysql_error($sql));
		}
		
	//Add user to group as admin
	$sql2 = "INSERT INTO " . GROUPS_JOINED_TABLE . " SET ".
		"groups_joined_name='" . $reg['group_name'] . "', ".
		"groups_joined_user_id='" . $USER->getId() . "', ".
		"groups_joined_date='" . $reg['group_date'] . "', ".
		"groups_joined_rank='5'";
	if(!$result = $db->sql_query($sql2))
		{
			printError("SQL ERROR", mysql_error($sql2));
		}
		else
		{
		echo"<br /><br /><br /><br /><br /><br /><br /><br /><br />
					<center><meta http-equiv='refresh' content='1;url=index.php?action=groups' />";
		echo "<h1>" . ucfirst(translate('group_added_successfully')) . "</h1>";
		echo "<br/>" . translate('redirectedin') . " <a href='index.php?action=groups>" . translate('clickhere') . "</a><div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
                </div></center>";
		exit;
		}
	}
}
?>

<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
        &gt; <a href="index.php?action=groups" title='<?php echo translate('groups'); ?>'><?php echo translate('groups'); ?></a> 
        &gt; <a href="index.php?action=groups&amp;sub=new" title='<?php echo translate('add_group'); ?>'><?php echo translate('add_group'); ?></a>
</div>

<br />
<br />

<center>
<form action="index.php?action=groups&amp;sub=new" method="post" name="groups_add" id="groups_add">
	<table>
    <tr>
    <td></td><td align="left"><h2><?php echo translate('add_group'); ?></h2></td>
	</tr>
    
    <tr>
			<td align="right"><b><?php echo ucf(translate('name')); ?></b></td>
            <td align="left"><input type="text" size="40" name="group_name" value="<?php echo $reg['group_name']; ?>" />
            <?php 
                if(isset($missing['group_name'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['group_name']) . "</span>";
                }
                ?>
                </td>
    </tr>
        	
    <tr>
			<td align="right"><b><?php echo ucf(translate('privacy')); ?></b></td>
            <td align="left">
            		<select name="group_rank">                    
              <?php
					foreach($PRIVACY_LIST as $k => $v)
					{
						if($v == $reg['group_rank'])
						{
							echo "<option value=\"" . $v . "\" selected=\"selected\">" . ucfirst(translate($v)) . "</option>\n";
						}
						else
						{
							echo "<option value=\"" . $v . "\">" . ucfirst(translate($v)) . "</option>\n";
						}
					}
					?>
                   </select>
             <?php 
                if(isset($missing['group_rank'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['group_rank']) . "</span>";
                }
                ?>
			</td>
    </tr>
    
    <tr>
    	<td align="right" valign="top"><b><?php echo ucf(translate('description')); ?></b></td>
        <td align="left">
        <textarea name="group_description" rows="5" cols="40"><?php echo $reg['group_description']; ?></textarea>
        <?php 
                if(isset($missing['group_description'])) 
                {
                    echo "<span class=\"error\"><-- " . translate($missing['group_description']) . "</span>";
                }
                ?>
        </td>
    </tr>
    
    <tr><td></td>
            <td align="left">
            <input style="font-weight:bold;" align="right" type="submit" name="groups_add_submit" value=" <?php echo ucf(translate('create_group')); ?> " />
			</td>
            
 	</tr>
	</table>
</form>
</center>